﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Media;

namespace ElisaHippo
{
    public class Star : Shape
    {
        // Using a DependencyProperty as the backing store for NumberOfPoints.      
        public static readonly DependencyProperty NumberOfPointsProperty =
        DependencyProperty.Register("NumberOfPoints", typeof(int), typeof(Shape), new UIPropertyMetadata(5));

        public int NumberOfPoints
        {
            get { return (int)GetValue(NumberOfPointsProperty); }
            set { SetValue(NumberOfPointsProperty, value); }
        }
    
        protected override Geometry DefiningGeometry
        {
            get { return VisualContainer.CreateStarGeometry(NumberOfPoints); }
        }

    }



    public class VisualContainer : Canvas
    {
        private List<Visual> visuals = new List<Visual>();

        public VisualContainer()
        {
            DrawingVisual star = CreateStar(5);

            visuals.Add(star);
        
            foreach (Visual visual in visuals)
            {
                AddVisualChild(visual);
                AddLogicalChild(visual);
            }
        }

        public static DrawingVisual CreateStar(int numberOfPoints)
        {
            DrawingVisual star = new DrawingVisual();

            using (DrawingContext drawingContext = star.RenderOpen())
            {
                Geometry outlinePath = CreateStarGeometry(numberOfPoints);
                drawingContext.DrawGeometry(Brushes.Gold, new Pen(Brushes.Black, 2), outlinePath);
            }

            return star;
        }

        public static Geometry CreateStarGeometry(int numberOfPoints)
        { 
            GeometryGroup group = new GeometryGroup();
            group.FillRule = FillRule.Nonzero;
            Geometry triangle = PathGeometry.Parse("M 0,-15 L 5,5 -5,5 0,-15");
            group.Children.Add(triangle);

            double deltaAngle = 360 / numberOfPoints;
            double currentAngle = 0;
            for (int index = 1; index < numberOfPoints; index++)
            {
                currentAngle += deltaAngle;
                triangle = triangle.CloneCurrentValue();
                triangle.Transform = new RotateTransform(currentAngle, 0, 0);
                group.Children.Add(triangle);
            }

            Geometry outlinePath = group.GetOutlinedPathGeometry();
            return outlinePath;

        }

        protected override int VisualChildrenCount
        {
            get { return visuals.Count; }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= visuals.Count)
                throw new ArgumentOutOfRangeException("index");

            return visuals[index];
        }

        public void AddStar()
        {
            Visual visual = CreateStar(5);
            visuals.Add(visual);
            PositionVisuals();
            AddVisualChild(visual);
            AddLogicalChild(visual);
        }

        private void PositionVisuals()
        {
            if (visuals.Count == 1)
                ((DrawingVisual)visuals[0]).Offset = new Vector(Width/2 , Height/2);
            else
            {
                double angle = 0;
                double deltaAngle = Math.PI * 2 / visuals.Count;
                double radius = Width / 2;
                foreach (DrawingVisual visual in visuals)
                {
                    visual.Offset = new Vector(Width / 2 + Math.Cos(angle) * radius, Height / 2 + Math.Sin(angle) * radius);
                    angle += deltaAngle;
                }
            }
        }
    }
}
